package cqu.zhaiyixin20215521.huarondao.slice.pintuInitialize;

import java.util.Random;

//随机生成一个有解的布局
public class RandomlyFill {
    public final int rowNum;
    public final int colNum;
    public final int[] pieces;
    private int spaceRow;
    private int spaceCol;
    private boolean spaceCanShiftUp, spaceCanShiftDown, spaceCanShiftLeft, spaceCanShiftRight;
    private final Random random = new Random(System.currentTimeMillis());

    public RandomlyFill(int rowNum, int colNum) {
        this.rowNum = rowNum;
        this.colNum = colNum;
        pieces = new int[rowNum * colNum];
        for(int i = 0; i < pieces.length; ++i) {
            pieces[i] = i;
        }
        spaceRow = (pieces.length - 1) / colNum;
        spaceCol = (pieces.length - 1) % colNum;
        spaceCanShiftUp = 0 < spaceRow;
        spaceCanShiftDown = spaceRow + 1 < rowNum;
        spaceCanShiftLeft = 0 < spaceCol;
        spaceCanShiftRight = spaceCol + 1 < colNum;
    }

    public void shuffle(int times) {
        for(int i = 0; i < times; ) {
            int direction = random.nextInt(4);
            int spaceIndex = spaceRow * colNum  + spaceCol;
            int pieceIndex;
            int tmp;
            switch (direction) {
                case 0:
                    if(spaceCanShiftUp) {
                        pieceIndex = spaceIndex - colNum;
                        tmp = pieces[spaceIndex];
                        pieces[spaceIndex] = pieces[pieceIndex];
                        pieces[pieceIndex] = tmp;
                        spaceRow--;
                        spaceCanShiftUp = 0 < spaceRow;
                        spaceCanShiftDown = true;
                        ++i;
                    }
                    break;
                case 1:
                    if(spaceCanShiftDown) {
                        pieceIndex = spaceIndex + colNum;
                        tmp = pieces[spaceIndex];
                        pieces[spaceIndex] = pieces[pieceIndex];
                        pieces[pieceIndex] = tmp;
                        spaceRow++;
                        spaceCanShiftUp = true;
                        spaceCanShiftDown = spaceRow + 1 < rowNum;
                        ++i;
                    }
                    break;
                case 2:
                    if(spaceCanShiftLeft) {
                        pieceIndex = spaceIndex - 1;
                        tmp = pieces[spaceIndex];
                        pieces[spaceIndex] = pieces[pieceIndex];
                        pieces[pieceIndex] = tmp;
                        spaceCol--;
                        spaceCanShiftLeft = 0 < spaceCol;
                        spaceCanShiftRight = true;
                        ++i;
                    }
                    break;
                case 3:
                    if(spaceCanShiftRight) {
                        pieceIndex = spaceIndex + 1;
                        tmp = pieces[spaceIndex];
                        pieces[spaceIndex] = pieces[pieceIndex];
                        pieces[pieceIndex] = tmp;
                        spaceCol++;
                        spaceCanShiftLeft = true;
                        spaceCanShiftRight = spaceCol + 1 < colNum;
                        ++i;
                    }
            }
        }
    }
}